/* icintr1.p - SUBPROGRAM TO FIND ACCT/CC FOR RCT-UNP/ISS-UNP INV TRANS       */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: icintr1.p 44578 2014-06-19 06:58:08Z aew                          $: */
/* Revision: 1.1     BY: Ed van de Gevel           DATE: 18/03/05 ECO: *R00F* */
/* Revision: 1.2     BY: Dan Herman                DATE: 04/14/06 ECO: *R02M* */
/* Revision: 1.3     BY: Jing Li                   DATE: 09/18/07 ECO: *R1BY* */
/* Revision: 1.4     BY: Yizhou Mao                DATE: 03/11/09 ECO: *R1DP* */
/* Revision: 1.5     BY: Steve Nugent              DATE: 03/17/09 ECO: *R1FC* */
/* Revision: 1.6     BY: Ambrose Almeida           DATE: 06/18/10 ECO: *R218* */
/*-Revision end---------------------------------------------------------------*/

{us/bbi/mfdeclre.i}
{us/gp/gpuid.i}
{us/bbi/gplabel.i}

define input  parameter transtype  as   character format "x(7)" no-undo.
define input  parameter p_site     as   character               no-undo.
define output parameter acct       like trgl_dr_acct            no-undo.
define output parameter sub        like trgl_dr_sub             no-undo.
define output parameter cctr       like trgl_dr_cc              no-undo.
define output parameter inv_mov    like im_inv_mov              no-undo.
define output parameter undo_all   like mfc_logical             no-undo.
define shared variable  lLegacyAPI as   logical                 no-undo.

define variable frame_euacct_title as   character      no-undo.
define variable l_auth             as   logical        no-undo.

/* COMMON API CONSTANTS AND VARIABLES */
{us/mf/mfaimfg.i}
/* INVENTORY CONTROL API DATASET DEF */
{us/ic/icdsicit.i "reference-only"}

frame_euacct_title = getFrameTitle("INVENTORY_MOVEMENT_CODE", 35).

for first shc_ctrl no-lock where
          shc_domain = global_domain:
end.

if lookup(transtype, "RCT-UNP,ISS-UNP") = 0
then
   return.

for first im_mstr no-lock where
          im_domain = global_domain
      and im_tr_type = transtype:
end.
if not available im_mstr
then
   return.

form
   skip(1)
   space(3) inv_mov space(3)
   skip(1)
with frame euacct side-labels view-as dialog-box
   title frame_euacct_title row 10 columns 20.
SetFrameLabels(frame euacct:handle).

assign
   acct   = ""
   sub    = ""
   cctr   = ""
   l_auth = yes.

/*WHEN IN API MODE CAN NOT PROMPT FOR VALUES*/
if c-application-mode = "API" and lLegacyAPI
then
   return.
else
   if not lLegacyAPI then
      {us/gp/gpapinit.i "dsInvTrans" "getRequestDataset"}

/* CHECK IF INV_MOV MATCHES THE SOURCE SITE & DESTINATION SITE */
for each sgid_det
   where  sgid_domain = global_domain
no-lock:

   for first im_mstr
      where im_domain  = sgid_domain
      and   im_inv_mov = sgid_inv_mov
      and   im_tr_type = transtype
   no-lock:
   end. /* FOR FIRST im_mstr */
   if avail im_mstr
   then
      leave.

end. /* FOR EACH sgid_det  */
if not available im_mstr
then
   return.

setacct:
do on error undo, retry on endkey undo, leave:

   if c-application-mode = "API" and not lLegacyAPI and retry then
      leave setacct.

   if c-application-mode <> "API" then
      set
         inv_mov
      with frame euacct.
   else
      if not lLegacyAPI then
         assign
            {us/mf/mfaiset.i inv_mov ttInvTrans.invMov}.

   if inv_mov <> "" then do:

      for first im_mstr no-lock where
                im_domain = global_domain
            and im_inv_mov = inv_mov
            and im_tr_type = transtype:
      end.

      if not available im_mstr
         and shc_require_inv_mov
      then do:
         {us/bbi/pxmsg.i &MSGNUM=5847
                  &ERRORLEVEL=3
                  &MSGARG1= transtype}
         /* INVENTORY MOVEMENT CODE WITH TRANSACTION TYPE # REQUIRED */
         undo, retry setacct.
      end.

      /* VALIDATE SITE/INVENTORY MOVEMENT CODE SECURITY */
      {us/bbi/gprun.i ""gpsimver.p""
         "(input  p_site,
           input  inv_mov,
           output l_auth)" }

      if not l_auth
      then do:
         /* USER DOES NOT HAVE ACCESS TO THIS SITE/INV MOV CODE */
         {us/bbi/pxmsg.i &MSGNUM=5990 &ERRORLEVEL=3}
         undo, retry setacct.
      end.  /* IF NOT l_auth */

      if available im_mstr
      then
         assign
            acct = im_unplanned_acct
            sub  = im_unplanned_sub
            cctr = im_unplanned_cc.

      do transaction:
         for first qad_wkfl exclusive-lock where
                   qad_domain = global_domain
               and qad_key1 = "INV_MOV_" + transtype
               and qad_key2 = SessionUniqueID:
         end.

         if not available qad_wkfl then do:
            create qad_wkfl.
            assign
               qad_domain = global_domain
               qad_key1 = "INV_MOV_" + transtype
               qad_key2 = SessionUniqueID.

            if recid(qad_wkfl) = -1 then.
         end.
         qad_charfld[1] = inv_mov.
      end.
   end.
   else do:
      if shc_require_inv_mov
      then do:
         {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
         undo, retry.
      end.
   end.
end.
if c-application-mode <> "API" and not lLegacyAPI then
   hide frame euacct no-pause.
undo_all = (keyfunction(lastkey) = "end-error").
